package com.proposition.paper.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.proposition.commons.constant.ExamCategory;
import com.proposition.commons.enums.CheckStatus;
import com.proposition.commons.enums.PaperPattern;
import com.proposition.commons.enums.PaperType;
import com.proposition.commons.model.*;
import com.proposition.exam.mapper.*;
import com.proposition.paper.mapper.PaperExamMapper;
import com.proposition.paper.mapper.PaperMapper;
import com.proposition.paper.mapper.SetAcupointMapper;
import com.proposition.paper.service.PaperAutoAddService;
import com.proposition.paper.service.PaperStrategyService;
import com.proposition.support.security.CurrentUser;
import com.proposition.support.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.sql.Timestamp;
import java.util.*;

/**
 * 自动组卷实现类
 *
 * @author 杨凯
 * @create 2018-02-05 15:26
 **/
@Service
public class PaperAutoAddServiceImpl implements PaperAutoAddService {


	private PaperExamMapper paperExamMapper;

	private PaperMapper paperMapper;

	private PaperStrategyService paperStrategyService;

	private ExamAcupointMapper examAcupointMapper;

	private ExamAnswer10Mapper examAnswer10Mapper;

	private ExamAnswer30Mapper examAnswer30Mapper;

	private ExamAcupointAdultMapper examAcupointAdultMapper;

	private ExamAcupointPediatricMapper examAcupointPediatricMapper;

	private SetAcupointMapper setAcupointMapper;

	private List<String> acupointList = null;

	@Autowired
	public PaperAutoAddServiceImpl(PaperExamMapper paperExamMapper, PaperMapper paperMapper, PaperStrategyService paperStrategyService, ExamAcupointMapper examAcupointMapper, ExamAnswer10Mapper examAnswer10Mapper, ExamAnswer30Mapper examAnswer30Mapper, ExamAcupointAdultMapper examAcupointAdultMapper, ExamAcupointPediatricMapper examAcupointPediatricMapper, SetAcupointMapper setAcupointMapper) {
		this.paperExamMapper = paperExamMapper;
		this.paperMapper = paperMapper;
		this.paperStrategyService = paperStrategyService;
		this.examAcupointMapper = examAcupointMapper;
		this.examAnswer10Mapper = examAnswer10Mapper;
		this.examAnswer30Mapper = examAnswer30Mapper;
		this.examAcupointAdultMapper = examAcupointAdultMapper;
		this.examAcupointPediatricMapper = examAcupointPediatricMapper;
		this.setAcupointMapper = setAcupointMapper;
	}

	/**
	 * 删除本年度所有试卷
	 */
	public void deleteAllPaperByYear() {

		//获取当前年度
		int year = Calendar.getInstance().get(Calendar.YEAR);
		//删除当前年度所有试卷试题
		List<String> paperIds = paperMapper.selectPaperIdByYear(year);
		if (!paperIds.isEmpty()) {
			paperExamMapper.deletePaperExamByPaperId(paperIds);
		}
		//删除当前年度试卷
		paperMapper.delete(new EntityWrapper<Paper>().eq("year", year));
	}

	@Override
	public void generateWendaPaper() {

		// 初始化随机数
		Random rand = new Random();
		// 获取组卷策略
		StrategyOne one = paperStrategyService.getStrategyOne();
		int exam2SK = one.getExam2SK();
		int exam2EK = one.getExam2EK();
		int exam2FK = one.getExam2FK();
		int exam2NK = one.getExam2NK();
		int exam2CZ = one.getExam2CZ();
		int exam3SK = one.getExam3SK();
		int exam3EK = one.getExam3EK();
		int exam3FK = one.getExam3FK();
		int exam3NK = one.getExam3NK();
		int exam3CZ = one.getExam3CZ();
		/**
		 * 如果答案相同的腧穴题 只选取一个
		 */
		List<ExamAcupoint> acupointOneBeans = examAcupointMapper.selectList(new EntityWrapper<ExamAcupoint>().eq("check_status_id", 4).groupBy("mayor_cure1,mayor_cure2,mayor_cure3"));
		/**
		 * 获取所有内、伤、妇、儿、触试题集合
		 */
		List<ExamAnswer10> exam2SKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.SHANG_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam2FKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.FU_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam2NKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.NEI_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam2EKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.ER_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam2CZList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.CHU_ZHEN_WD).groupBy("answer1,answer2,answer3"));
		/**
		 * 获取所有内、伤、妇、儿、触试题集合
		 */
		List<ExamAnswer10> exam3SKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.SHANG_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam3FKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.FU_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam3NKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.NEI_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam3EKList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.ER_KE_WD).groupBy("answer1,answer2,answer3"));
		List<ExamAnswer10> exam3CZList = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.CHU_ZHEN_WD).groupBy("answer1,answer2,answer3"));
		//生成第一站第二题
		ExamAnswer10 examinationBean2 = null;
		List<ExamAnswer10> exam2All = new ArrayList<>();
		// 第一站第二题
		for (int j = 0; j < exam2SK; j++) {
			int exam2Rand = rand.nextInt(exam2SKList.size());
			examinationBean2 = exam2SKList.get(exam2Rand);
			exam2SKList.remove(exam2Rand);
			exam2All.add(examinationBean2);
		}
		for (int j = 0; j < exam2NK; j++) {
			int exam2Rand = rand.nextInt(exam2NKList.size());
			examinationBean2 = exam2NKList.get(exam2Rand);
			exam2NKList.remove(exam2Rand);
			exam2All.add(examinationBean2);
		}
		for (int j = 0; j < exam2FK; j++) {
			int exam2Rand = rand.nextInt(exam2FKList.size());
			examinationBean2 = exam2FKList.get(exam2Rand);
			exam2FKList.remove(exam2Rand);
			exam2All.add(examinationBean2);
		}
		for (int j = 0; j < exam2EK; j++) {
			int exam2Rand = rand.nextInt(exam2EKList.size());
			examinationBean2 = exam2EKList.get(exam2Rand);
			exam2EKList.remove(exam2Rand);
			exam2All.add(examinationBean2);
		}
		for (int j = 0; j < exam2CZ; j++) {
			int exam2Rand = rand.nextInt(exam2CZList.size());
			examinationBean2 = exam2CZList.get(exam2Rand);
			exam2CZList.remove(exam2Rand);
			exam2All.add(examinationBean2);
		}
		// 生成第一站第三题

		List<ExamAnswer10> exam3All = new ArrayList<>();
		ExamAnswer10 examinationBean3 = null;
		for (int j = 0; j < exam3SK; j++) {
			int exam3Rand = rand.nextInt(exam3SKList.size());
			examinationBean3 = exam3SKList.get(exam3Rand);
			exam3SKList.remove(exam3Rand);
			exam3All.add(examinationBean3);
		}
		for (int j = 0; j < exam3NK; j++) {
			int exam3Rand = rand.nextInt(exam3NKList.size());
			examinationBean3 = exam3NKList.get(exam3Rand);
			exam3NKList.remove(exam3Rand);
			exam3All.add(examinationBean3);
		}
		for (int j = 0; j < exam3FK; j++) {
			int exam3Rand = rand.nextInt(exam3FKList.size());
			examinationBean3 = exam3FKList.get(exam3Rand);
			exam3FKList.remove(exam3Rand);
			exam3All.add(examinationBean3);
		}
		for (int j = 0; j < exam3EK; j++) {
			int exam3Rand = rand.nextInt(exam3EKList.size());
			examinationBean3 = exam3EKList.get(exam3Rand);
			exam3EKList.remove(exam3Rand);
			exam3All.add(examinationBean3);
		}
		for (int j = 0; j < exam3CZ; j++) {
			int exam3Rand = rand.nextInt(exam3CZList.size());
			examinationBean3 = exam3CZList.get(exam3Rand);
			exam3CZList.remove(exam3Rand);
			exam3All.add(examinationBean3);
		}

		for (int i = 0; i < 40; i++) {
			//从腧穴题集合中随机获取一道题
			int exam1Rand = rand.nextInt(acupointOneBeans.size());
			ExamAcupoint acupointOneBean = acupointOneBeans
					.get(exam1Rand);
			//从集合中移除已选择的腧穴题
			acupointOneBeans.remove(exam1Rand);

			// 第一站第二题
			int exam2Rand = rand.nextInt(exam2All.size());
			examinationBean2 = exam2All.get(exam2Rand);
			exam2All.remove(exam2Rand);

			// 第一站第三题
			int exam3Rand = rand.nextInt(exam3All.size());
			examinationBean3 = exam3All.get(exam3Rand);
			exam3All.remove(exam3Rand);

			// 第一题
			String examOne = acupointOneBean.getId();
			// 第二题
			String examTwo = examinationBean2.getId();
			// 第三题
			String examThree = examinationBean3.getId();
			// 获取试卷名称
			String paperId = UUIDUtil.getPaperUUID(PaperPattern.WEN_DA);
			String paperName = "";
			Paper paper = new Paper();
			paper.setId(paperId);
			paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
			paper.setCreateTime(new Timestamp(System.currentTimeMillis()));
			paper.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			//获取当前用户
			paper.setUsername(CurrentUser.INSTANCE.getUserName());
			paper.setPaperPattern(PaperPattern.WEN_DA);
			paper.setCheckStatus(CheckStatus.WEI_SHEN_HE);
			if (i < 20) {
				//生成A卷试卷名称
				paperName = generatePaperName(PaperPattern.WEN_DA, PaperType.A);
				int batch = generatePaperBatch(PaperPattern.WEN_DA, PaperType.A);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.WEN_DA, PaperType.A));
				paper.setType(PaperType.A);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			} else {
				//生成B卷试卷名称
				paperName = generatePaperName(PaperPattern.WEN_DA, PaperType.B);
				int batch = generatePaperBatch(PaperPattern.WEN_DA, PaperType.B);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.WEN_DA, PaperType.B));
				paper.setType(PaperType.B);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			}
			PaperExam paperExam = new PaperExam();
			paperExam.setId(UUIDUtil.getUUID());
			paperExam.setPaperId(paperId);
			paperExam.setExam1(examOne);
			paperExam.setExam2(examTwo);
			paperExam.setExam3(examThree);
			//插入试卷试题
			paperExamMapper.insert(paperExam);
		}

	}

	/**
	 * 第二站常规化第二题 组卷策略为   ：
	 * 选择同一条经络 同一个部位的穴位不能再选
	 */
	@Override
	public void generateChangGuiHuaPaper() {
		// 初始化随机数
		Random rand = new Random();
		StrategyTwo two = paperStrategyService.getStrategyTwo();
		// 获取第一题组卷策略
		String[] exam1 = two.getExam1().split(",");
		String[] exam2 = two.getExam2().split(",");
		// 第三题
		// 伤科
		int sk = two.getExamSK();
		// 儿科
		int ek = two.getExamEK();
		// 妇科
		int fk = two.getExamFK();
		// 内科
		int nk = two.getExamNK();
		// 内科列表
		List<ExamAnswer30> nkList = examAnswer30Mapper.selectList(new EntityWrapper<ExamAnswer30>().eq("check_status_id", 4).eq("category_id", ExamCategory.NEI_KE_ZT));
		// 伤科列表
		List<ExamAnswer30> skList = examAnswer30Mapper.selectList(new EntityWrapper<ExamAnswer30>().eq("check_status_id", 4).eq("category_id", ExamCategory.SHANG_KE_ZT));
		// 妇科列表
		List<ExamAnswer30> fkList = examAnswer30Mapper.selectList(new EntityWrapper<ExamAnswer30>().eq("check_status_id", 4).eq("category_id", ExamCategory.FU_KE_ZT));
		// 儿科列表
		List<ExamAnswer30> ekList = examAnswer30Mapper.selectList(new EntityWrapper<ExamAnswer30>().eq("check_status_id", 4).eq("category_id", ExamCategory.ER_KE_ZT));
		List<ExamAnswer30> beans = new ArrayList<ExamAnswer30>();
		// 随机获取一个伤科整套试题
		for (int i = 0; i < sk; i++) {
			int skrand = rand.nextInt(skList.size());
			ExamAnswer30 examinationBean = skList.get(skrand);
			skList.remove(skrand);
			beans.add(examinationBean);
		}
		// 随机获取一个儿科整套试题
		for (int i = 0; i < ek; i++) {
			int ekrand = rand.nextInt(ekList.size());
			ExamAnswer30 examinationBean = ekList.get(ekrand);
			ekList.remove(ekrand);
			beans.add(examinationBean);
		}
		// 随机获取一个妇科整套试题
		for (int i = 0; i < fk; i++) {
			int fkrand = rand.nextInt(fkList.size());
			ExamAnswer30 examinationBean = fkList.get(fkrand);
			fkList.remove(fkrand);
			beans.add(examinationBean);
		}
		// 随机获取一个内科整套试题
		for (int i = 0; i < nk; i++) {
			int nkrand = rand.nextInt(nkList.size());
			ExamAnswer30 examinationBean = nkList.get(nkrand);
			nkList.remove(nkrand);
			beans.add(examinationBean);
		}
		// 获取第三题列表为了与客观化同步
		beans.addAll(beans);

		for (int i = 0; i < 40; i++) {
			// 儿科手法
			List<ExamAnswer10> erkesf = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.ER_KE_SF).groupBy("answer1,answer2,answer3"));
			//触诊
			List<ExamAnswer10> chuzhen = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.CHU_ZHEN_ZD).groupBy("answer1,answer2,answer3"));
			StringBuffer exam1Id = new StringBuffer();

			List<ExamAcupointAdult> adults = new ArrayList<>();
			List<ExamAcupointPediatric> pediatrics = new ArrayList<>();

			for (String category : exam1) {
				if (category.equals(ExamCategory.CHENG_REN_XW)) {
					ExamAcupointAdult examAcupointAdult = getExamAcupointAdult(adults);
					exam1Id.append(examAcupointAdult.getId()).append(",");
					adults.add(examAcupointAdult);
				} else if (category.equals(ExamCategory.ER_KE_XW)) {
					ExamAcupointPediatric examAcupointPediatric = getExamAcupointPediatric(pediatrics);
					exam1Id.append(examAcupointPediatric.getId()).append(",");
					pediatrics.add(examAcupointPediatric);
				}
			}
			StringBuffer exam2Id = new StringBuffer();
			List<ExamAnswer10> examAnswer10List = new ArrayList<>();
			for (String category : exam2) {
				if (category.equals(ExamCategory.CHENG_REN_SF)) {
					ExamAnswer10 examAnswer10 = getChengRenShouFa(examAnswer10List);
					exam2Id.append(examAnswer10.getId()).append(",");
					examAnswer10List.add(examAnswer10);
				} else if (category.equals(ExamCategory.ER_KE_SF)) {
					int random = rand.nextInt(erkesf.size());
					ExamAnswer10 acupointTwoBean = erkesf.get(random);
					erkesf.remove(random);
					exam2Id.append(acupointTwoBean.getId()).append(",");
				} else if (category.equals(ExamCategory.CHU_ZHEN_ZD)) {
					int random = rand.nextInt(chuzhen.size());
					ExamAnswer10 acupointTwoBean = chuzhen.get(random);
					chuzhen.remove(random);
					exam2Id.append(acupointTwoBean.getId()).append(",");
				}
			}
			// 获取试卷名称
			String paperId = UUIDUtil.getPaperUUID(PaperPattern.CHANG_GUI);
			String paperName = "";
			Paper paper = new Paper();
			paper.setId(paperId);
			paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
			paper.setCreateTime(new Timestamp(System.currentTimeMillis()));
			paper.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			paper.setUsername(CurrentUser.INSTANCE.getUserName());
			paper.setPaperPattern(PaperPattern.CHANG_GUI);
			paper.setCheckStatus(CheckStatus.WEI_SHEN_HE);
			if (i < 20) {
				paperName = generatePaperName(PaperPattern.CHANG_GUI, PaperType.A);
				int batch = generatePaperBatch(PaperPattern.CHANG_GUI, PaperType.A);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.CHANG_GUI, PaperType.A));
				paper.setType(PaperType.A);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			} else {
				paperName = generatePaperName(PaperPattern.CHANG_GUI, PaperType.B);
				int batch = generatePaperBatch(PaperPattern.CHANG_GUI, PaperType.B);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.CHANG_GUI, PaperType.B));
				paper.setType(PaperType.B);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			}
			PaperExam paperExam = new PaperExam();
			paperExam.setId(UUIDUtil.getUUID());
			paperExam.setPaperId(paperId);
			paperExam.setExam1(exam1Id.toString());
			paperExam.setExam2(exam2Id.toString());
			paperExam.setExam3(beans.get(i).getId());
			paperExamMapper.insert(paperExam);
		}

		generateKeGuanHuaPaper(beans);

	}

	@Override
	public void generateKeGuanHuaPaper(List<ExamAnswer30> examThree) {
		acupointList = new ArrayList<>();
		Random random = new Random();
		for (int j = 0; j < 40; j++) {
			if(j == 20){
				acupointList.clear();
			}
			// 第二题单项手法
			List<ExamAnswer10> techniques = new ArrayList<ExamAnswer10>();
			List<ExamAnswer10> techniquesNew = new ArrayList<ExamAnswer10>();
			// 由于有七个分类
			for (int i = 1; i < 8; i++) {
				List<ExamAnswer10> list2 = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("single_ploy_five_category_id", i).eq("check_status_id", 4).groupBy("answer1,answer2,answer3"));
				if (list2 == null || list2.isEmpty()) {
					continue;
				}
				int rd = random.nextInt(list2.size());
				techniques.add(list2.get(rd));
			}

			// 随机取出四个题
			for (int i = 0; i < 4; i++) {
				int rd = random.nextInt(techniques.size());
				techniquesNew.add(techniques.get(rd));
				techniques.remove(rd);
			}
			// 将试题Id取出来
			String examOne = getExamKeGuanHuaAcupoint();
			StringBuffer examTwo = new StringBuffer();
			for (ExamAnswer10 technique : techniquesNew) {
				examTwo.append(technique.getId()).append(",");
			}
			// 获取试卷名称
			String paperId = UUIDUtil.getPaperUUID(PaperPattern.KE_GUAN);
			String paperName = "";
			Paper paper = new Paper();
			paper.setId(paperId);
			paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
			paper.setCreateTime(new Timestamp(System.currentTimeMillis()));
			paper.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			paper.setUsername(CurrentUser.INSTANCE.getUserName());
			paper.setPaperPattern(PaperPattern.KE_GUAN);
			paper.setCheckStatus(CheckStatus.WEI_SHEN_HE);
			if (j < 20) {
				paperName = generatePaperName(PaperPattern.KE_GUAN, PaperType.A);
				int batch = generatePaperBatch(PaperPattern.KE_GUAN, PaperType.A);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.KE_GUAN, PaperType.A));
				paper.setType(PaperType.A);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			} else {
				paperName = generatePaperName(PaperPattern.KE_GUAN, PaperType.B);
				int batch = generatePaperBatch(PaperPattern.KE_GUAN, PaperType.B);
				paper.setName(paperName);
				paper.setNumber(generatePaperNumber(PaperPattern.KE_GUAN, PaperType.B));
				paper.setType(PaperType.B);
				paper.setBatch(batch);
				paperMapper.insert(paper);
			}
			PaperExam paperExam = new PaperExam();
			paperExam.setId(UUIDUtil.getUUID());
			paperExam.setPaperId(paperId);
			paperExam.setExam1(examOne);
			paperExam.setExam2(examTwo.toString());
			paperExam.setExam3(examThree.get(j).getId());
			paperExamMapper.insert(paperExam);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void generateAllPatternPaper() {
		//删除本年度所有试题
		//deleteAllPaperByYear();
		//生成第一站
		generateWendaPaper();
		//生成第二站和第三站
		generateChangGuiHuaPaper();
	}


	/**
	 * 生成客观化穴位定位
	 *  穴位生成规则为：部位顺序为从上往下，从前往后，不能是相同归经和相同部位的穴位
	 *  部位id 基数为 前面部位   偶数为后面部位
	 *  排序规则为：将10个穴位分为两个集合 (前面和后面，根据取余数求得),然后再根据穴位顺序排序，最后将两个集合合并一个集合，再用逗号拼接成字符串。
	 *
	 * @return
	 */
	public String getExamKeGuanHuaAcupoint() {
		Random random = new Random();
		List<ExamAcupointAdult> list = new ArrayList<ExamAcupointAdult>();
		List<Integer> list1 = new ArrayList<>();
		List<String> list2 = new ArrayList<>();
		List<ExamAcupointAdult> list3 = new ArrayList<ExamAcupointAdult>();
		List<ExamAcupointAdult> front = new ArrayList<ExamAcupointAdult>();
		List<ExamAcupointAdult> behind = new ArrayList<ExamAcupointAdult>();
		List<ExamAcupointAdult> listNew = new ArrayList<ExamAcupointAdult>();
		boolean condition = true;
		list = examAcupointAdultMapper.selectList(new EntityWrapper<>());
		AcupointSort acupointSort = new AcupointSort();
		for (ExamAcupointAdult adult : list) {
			acupointSort = acupointSort.selectOne(new EntityWrapper().eq("title", adult.getStem()));
			adult.setPartIdAdult(acupointSort.getPart());
			list3.add(adult);
		}
		//生成总时间设置为十秒
		//开始时间
		long startTime = System.currentTimeMillis();
		//十秒
		long time = 10 * 1000;
		long endTime = startTime + time;
		int i = 1;
		while (condition) {
			if (!(System.currentTimeMillis() - endTime > 0)) {
				int rd = random.nextInt(list3.size());
				if (!list1.contains(list3.get(rd).getPartIdAdult()) && !list2.contains(list3.get(rd).getMeridianAdult())) {
					//全部穴位不能出现三次
					//判断获取的试题Id 出现次数
					int frequency = Collections.frequency(acupointList, list3.get(rd).getId());
					if (frequency > 3) {
						return getExamKeGuanHuaAcupoint();
					}
					int partId = list3.get(rd).getPartIdAdult();
					list1.add(partId);
					list2.add(list3.get(rd).getMeridianAdult());
					if (partId % 2 == 0) {
						behind.add(list3.get(rd));
					} else {
						front.add(list3.get(rd));
					}
					acupointList.add(list3.get(rd).getId());
					listNew.add(list3.get(rd));
					list3.remove(rd);
				}
				if (listNew.size() == 10) {
					condition = false;
				}
				i++;
			} else {
				System.out.println("------十秒内未生成试卷进行递归");
				return getExamKeGuanHuaAcupoint();
			}
		}
		// 通过部位排序
		Collections.sort(behind, new Comparator<ExamAcupointAdult>() {
			/*
			 * int compare(Student o1, Student o2) 返回一个基本类型的整型， 返回负数表示：o1
			 * 小于o2， 返回0 表示：o1和o2相等， 返回正数表示：o1大于o2。
			 */
			@Override
			public int compare(ExamAcupointAdult a1, ExamAcupointAdult a2) {
				if (a1.getPartIdAdult() > a2.getPartIdAdult()) {
					return 1;
				} else if (a1.getPartIdAdult().equals(a2.getPartIdAdult())) {
					return 0;
				} else {
					return -1;
				}
			}
		});
		// 通过部位排序
		Collections.sort(front, new Comparator<ExamAcupointAdult>() {
			@Override
			public int compare(ExamAcupointAdult a1, ExamAcupointAdult a2) {
				if (a1.getPartIdAdult() > a2.getPartIdAdult()) {
					return 1;
				} else if (a1.getPartIdAdult().equals(a2.getPartIdAdult())) {
					return 0;
				} else {
					return -1;
				}
			}
		});

		front.addAll(behind);
		// 将试题Id取出来
		StringBuffer examOne = new StringBuffer();
		StringBuffer examOne1 = new StringBuffer();
		//用逗号将穴位Id拼接起来
		for (ExamAcupointAdult acupointThree : front) {
			examOne1.append(acupointThree.getPartIdAdult()).append(",");
			examOne.append(acupointThree.getId()).append(",");
		}
		examOne1.append("循环:").append(i).append("次");
		System.out.println(examOne1.toString());
		return examOne.toString();
	}


	/**
	 * 获取一个成人穴位试题
	 *
	 * @return
	 */
	public ExamAcupointAdult getExamAcupointAdult(List<ExamAcupointAdult> list) {
		// 初始化随机数
		Random rand = new Random();
		// 成人穴位
		List<ExamAcupointAdult> chengrenxw = examAcupointAdultMapper.selectList(new EntityWrapper<ExamAcupointAdult>().eq("check_status_id", 4));
		int random = rand.nextInt(chengrenxw.size());
		ExamAcupointAdult examAcupointAdult = chengrenxw
				.get(random);
		if (!list.isEmpty()) {
			for (ExamAcupointAdult adult : list) {

				if (examAcupointAdult.getPartIdAdult().equals(adult.getPartIdAdult())) {
					//递归生成
					return getExamAcupointAdult(list);
				}
				//如果经络相同 则重新生成
				if (examAcupointAdult.getMeridianAdult().equals(adult.getMeridianAdult())) {
					//递归生成
					return getExamAcupointAdult(list);
				}
			}
		}
		return examAcupointAdult;
	}


	/**
	 * 获取儿科穴位
	 *
	 * @param pediatrics
	 * @return
	 */

	public ExamAcupointPediatric getExamAcupointPediatric(List<ExamAcupointPediatric> pediatrics) {
		// 初始化随机数
		Random rand = new Random();
		// 儿科穴位
		List<ExamAcupointPediatric> erkexw = examAcupointPediatricMapper.selectList(new EntityWrapper<ExamAcupointPediatric>().eq("check_status_id", 4));
		int random = rand.nextInt(erkexw.size());
		ExamAcupointPediatric examAcupointPediatric = erkexw
				.get(random);
		if (!pediatrics.isEmpty()) {
			for (ExamAcupointPediatric pediatric : pediatrics) {
				//如归再次生成的试题和之前生成的试题部位相同，则重新再生成
				if (examAcupointPediatric.getPartIdPediatric().equals(pediatric.getPartIdPediatric())) {
					//递归生成
					return getExamAcupointPediatric(pediatrics);
				}
			}
		}
		return examAcupointPediatric;
	}


	/**
	 * 获取成人手法
	 *
	 * @return
	 */
	public ExamAnswer10 getChengRenShouFa(List<ExamAnswer10> list) {
		// 初始化随机数
		Random rand = new Random();
		// 成人手法
		List<ExamAnswer10> chengrensf = examAnswer10Mapper.selectList(new EntityWrapper<ExamAnswer10>().eq("check_status_id", 4).eq("category_id", ExamCategory.CHENG_REN_SF).groupBy("answer1,answer2,answer3"));
		int random = rand.nextInt(chengrensf.size());
		ExamAnswer10 answer10 = chengrensf.get(random);
		if (!list.isEmpty()) {
			for (ExamAnswer10 examAnswer10 : list) {
				if (examAnswer10.getSinglePloyFiveCategoryId().equals(answer10.getSinglePloyFiveCategoryId())) {
					return getChengRenShouFa(list);
				}
			}
		}
		return answer10;
	}

	/**
	 * 生成试卷编号
	 *
	 * @param paperPattern
	 * @param paperType
	 * @return
	 */
	public String generatePaperNumber(PaperPattern paperPattern, PaperType paperType) {
		String paperName = "";
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int batch = generatePaperBatch(paperPattern, paperType);
		//根据站类型和试卷A/B 卷查询试卷已经组了多少套
		Wrapper wrapper = new EntityWrapper<Paper>();
		wrapper.eq("paper_pattern", paperPattern)
				.eq("type", paperType)
				.eq("year", year)
				.eq("batch", batch);
		int count = paperMapper.selectCount(wrapper);
		if (paperType == PaperType.A) {
			paperName = "A" + String.valueOf(count + 1);
		} else if (paperType == PaperType.B) {
			paperName = "B" + String.valueOf(count + 1);
		}
		return paperName;
	}

	/**
	 * 生成试卷名称
	 *
	 * @param paperPattern 站类型
	 * @param paperType    试卷A/B 卷
	 * @return
	 */
	public String generatePaperName(PaperPattern paperPattern, PaperType paperType) {
		StringBuffer name = new StringBuffer();
		if (paperPattern == PaperPattern.WEN_DA) {
			name.append("实践第一站");
		} else if (paperPattern == PaperPattern.CHANG_GUI) {
			name.append("实践第二站");
		} else if (paperPattern == PaperPattern.KE_GUAN) {
			name.append("实践第二站客观化");
		}
		String number = generatePaperNumber(paperPattern, paperType);
		name.append(number);
		return name.toString();
	}

	/**
	 * 生成试卷批次
	 *
	 * @param pattern
	 * @param paperType
	 * @return
	 */
	public int generatePaperBatch(PaperPattern pattern, PaperType paperType) {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int batch;
		int count;
		//获取最新的批次
		Paper paper = paperMapper.selectBatch(pattern, paperType, year);

		Wrapper wrapper = new EntityWrapper<Paper>();
		wrapper.eq("paper_pattern", pattern)
				.eq("type", paperType)
				.eq("year", year);
		if (!ObjectUtils.isEmpty(paper)) {
			wrapper.eq("batch", paper.getBatch());
		}
		count = paperMapper.selectCount(wrapper);
		if (count == 0) {
			//如果本年度没有进行组卷，那么批次为1
			batch = 1;
		} else if (count == 20) {
			batch = paper.getBatch() + 1;
		} else {
			batch = paper.getBatch();
		}

		return batch;
	}

	/**
	 * 首先进行入参检查防止出现空指针异常
	 * 如果两个参数都为空，则返回true
	 * 如果有一项为空，则返回false
	 * 接着对第一个list进行遍历，如果某一项第二个list里面没有，则返回false
	 * 还要再将两个list反过来比较，因为可能一个list是两一个list的子集
	 * 如果成功遍历结束，返回true
	 *
	 * @param l0
	 * @param l1
	 * @return
	 */
	public boolean isListEqual(List l0, List l1) {
		if (l0 == l1) {
			return true;
		}
		if (l0 == null && l1 == null) {
			return true;
		}
		if (l0 == null || l1 == null) {
			return false;
		}
		if (l0.size() != l1.size()) {
			return false;
		}
		for (Object o : l0) {
			if (!l1.contains(o)) {
				return false;
			}
		}
		for (Object o : l1) {
			if (!l0.contains(o)) {
				return false;
			}
		}
		return true;
	}
}
